package com.hilihili.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hilihili.mapper.UsersMapper;
import com.hilihili.domain.entity.Users;
import com.hilihili.service.IUsersService;

/**
 * 前端后台用户Service业务层处理
 *
 * @author xhc
 * @date 2023-11-14
 */
@Service
public class UsersServiceImpl implements IUsersService
{
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private RedisCache redisCache;

    @Override
    public String selectUsersByUp(Long up) {
        return usersMapper.selectUsersByUp(up);
    }

    @Override
    public Users selectUsersByUsername(String username) {
        return usersMapper.selectUsersByUsername(username);
    }
    /**
     * 注册
     */
    public String register(RegisterBody registerBody)
    {
        String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword();
        Users u = new Users();
        int code = 200;
        Users users = usersMapper.selectUsersByUsername(username);

        // 验证码开关
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        if (captchaEnabled)
        {
            validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
        }

        //前端同时也验证。规则可以自己定义
        if (StringUtils.isEmpty(username))
        {
            msg = "用户名不能为空";
            code = 101;
        }
        else if (StringUtils.isEmpty(password))
        {
            msg = "用户密码不能为空";
            code = 101;
        }
        else if (username.length() < UserConstants.USERNAME_OTHER
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在6到20个字符之间";
            code = 103;
        }
        else if (password.length() < UserConstants.PASSWORD_OTHER
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在6到20个字符之间";
            code = 103;
        }
        else if (users != null)
        {
            msg = "保存用户'" + username + "'失败，注册账号已存在";
            code = 102;
        }
        else
        {
            u.setUsername(username);
            u.setNickname(username);
            u.setPassword(password);
            //设置默认头像地址
            u.setAvatar("https://i0.hdslb.com/bfs/face/3f1496f48a5eb840a974fef0fd7db7a3f225fbe7.jpg");
            u.setCreatedAt(new Date());
            int regFlag = usersMapper.insertUsers(u);
            if (regFlag == 0)
            {
                msg = "注册失败,请联系系统管理人员";
            }else {
                msg = "注册成功";
            }

        }
        return msg;
    }

    @Override
    public Users selectUsersByUserId(Long userId) {
        return usersMapper.selectUsersByUserId(userId);
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public void validateCaptcha(String username, String code, String uuid)
    {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null)
        {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha))
        {
            throw new CaptchaException();
        }
    }

//    -----------------------------------------------------
    /**
     * 查询前端后台用户
     *
     * @param id 前端后台用户主键
     * @return 前端后台用户
     */
    @Override
    public Users selectUsersById(Long id)
    {
        return usersMapper.selectUsersById(id);
    }

    /**
     * 查询前端后台用户列表
     *
     * @param users 前端后台用户
     * @return 前端后台用户
     */
    @Override
    public List<Users> selectUsersList(Users users)
    {
        return usersMapper.selectUsersList(users);
    }

    /**
     * 新增前端后台用户
     *
     * @param users 前端后台用户
     * @return 结果
     */
    @Override
    public int insertUsers(Users users)
    {
        return usersMapper.insertUsers(users);
    }

    /**
     * 修改前端后台用户
     *
     * @param users 前端后台用户
     * @return 结果
     */
    @Override
    public int updateUsers(Users users)
    {
        return usersMapper.updateUsers(users);
    }

    /**
     * 批量删除前端后台用户
     *
     * @param ids 需要删除的前端后台用户主键
     * @return 结果
     */
    @Override
    public int deleteUsersByIds(Long[] ids)
    {
        return usersMapper.deleteUsersByIds(ids);
    }

    /**
     * 删除前端后台用户信息
     *
     * @param id 前端后台用户主键
     * @return 结果
     */
    @Override
    public int deleteUsersById(Long id)
    {
        return usersMapper.deleteUsersById(id);
    }
}
